//
//  StatusOriginalView.swift
//  sinaweibo
//
//  Created by mac on 15/11/14.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class StatusOriginalView: UIView {

    //原创微博的底部约束, 根据是否有配图进行修改
    var bottomConstraints: Constraint?
    
    var status:Status? {
        didSet{
            iconImage.sd_setImageWithURL(status?.user?.headImageURl, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            memberIconView.image = status?.user?.mbrankImage
            verifiedIconView.image = status?.user?.verified_type_Image
            timeLabel.text = status?.created_at //"11:34"
            sourceLabel.text = "糗事百科" //status?.source
            contentLabel.text = status?.text
            //有配图 就将原创微博的底部视图设置为 配图视图的底部  没有就以微博正文的底部为约束
            //移除约束的方法,在更新之前先移除,要不xcode这2b不知道到底是哪个
            self.bottomConstraints?.uninstall()
            if let pictureUrl = status?.imageURLs where pictureUrl.count > 0 {
                
                pictureView.imageURLs = pictureUrl
                
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                })
                
            }else{
                pictureView.hidden = true

                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(statusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
//        backgroundColor = UIColor.darkGrayColor()
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(verifiedIconView)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        addSubview(sepView)
        //设置约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(statusCellMargin)
        }
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(snp_left).offset(statusCellMargin)
            make.width.equalTo(statusIconWidth)
            make.height.equalTo(statusIconWidth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(statusCellMargin)
        }
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(nameLabel.snp_left)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(iconImage.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        }
        
        //将原创微博的底部与微博正文的底部对齐
        self.snp_makeConstraints { (make) -> Void in
            //将约束条件 修改成约束类型  .constraint
                self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
    }
    
    //添加每个cell之间的分割线,增强层次感
    private lazy var sepView:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.85, alpha: 1)
        return v
    }()
    
    //头像
    private lazy var iconImage:UIImageView = {
        let icon = UIImageView()
        icon.layer.cornerRadius = 17
        icon.clipsToBounds = true
        return icon
    }()
    //用户名
    private lazy var nameLabel:UILabel = UILabel(title: "杨xming", size: 14, color: UIColor.lightGrayColor())
    //用户认证类型
    private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级
    private lazy var memberIconView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 10, color: UIColor.orangeColor())
    ///微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "来自: 一道光", size: 10, color: UIColor.lightGrayColor())
    ///微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "野内心的攻城狮野内心的攻城狮野内心的攻城狮野内心的攻城狮野内心的攻城狮野内心的攻城狮野内心的攻城狮攻城狮野内心的攻城狮攻城狮野内心的攻城狮攻城狮野内心的攻城狮", size: 14, color: UIColor.lightGrayColor(), margin: statusCellMargin)
    //视图
    private lazy var pictureView:StatusPictureView = StatusPictureView()
    
  
}